package com.jianzh5.util;

import com.google.common.collect.Maps;
import com.jianzh5.pojo.PAdmin;
import com.jianzh5.pojo.PResources;
import com.jianzh5.service.RoleServiceI;
import com.jianzh5.service.UserServiceI;
import com.jianzh5.shiro.SystemAuthorizingRealm.Principal;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;

import java.util.List;
import java.util.Map;

/**
 * 用户工具类,获取用户权限
 * @author jianzh5
 */
public class UserUtils {
	
	//private static BaseDaoI<TAdmin> adminDao = SpringContextHolder.getBean("baseDao");
	private static UserServiceI userService = SpringContextHolder.getBean("userService");
	private static RoleServiceI roleService = SpringContextHolder.getBean("roleService");
	
	public static final String CACHE_USER = "adminUser";
	
	public static PAdmin getUser(){
		
		PAdmin adminUser = (PAdmin) getCache(CACHE_USER);
		if(adminUser == null){
			try{
				Subject subject = SecurityUtils.getSubject();
				Principal principal = (Principal) subject.getPrincipal();
				if(principal != null){
					adminUser = userService.getPadmin(principal.getId());
					putCache(CACHE_USER, adminUser);
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(adminUser == null){
			adminUser = new PAdmin();
			try{
				SecurityUtils.getSubject().logout();
			}catch (Exception e) {
				
			}
		}
		return adminUser;
	}
	
	public static PAdmin getUser(boolean isRefresh){
		if (isRefresh){
			removeCache(CACHE_USER);
		}
		return getUser();
	}
	
	/**
	 * 查询当前用户的菜单
	 * 从User对象中取出Resources
	 * @return
	 */
	public static List<PResources> getResourcesList(){
		@SuppressWarnings("unchecked")
		List<PResources> resourcesList = (List<PResources>) getCache("resourcesList");
		if(resourcesList == null){
			resourcesList = getUser().getMenuList();
		}
		putCache("resourcesList", resourcesList);
		return resourcesList;
	}
	
	/**
	 * 获取系统所有资源并放入缓存中
	 * @return
	 */
	public static List<PResources> getResourcesTree(){
		@SuppressWarnings("unchecked")
		List<PResources> resourcesTree = (List<PResources>) getCache("resourcesTree");
		if(resourcesTree == null){
			resourcesTree = roleService.getRoleTree();
		}
		putCache("resourcesTree", resourcesTree);
		return resourcesTree;
	}
	
	//============================用户Cache类================================
	public static Object getCache(String key) {
		return getCache(key, null);
	}
	
	public static Object getCache(String key, Object defaultValue) {
		Object obj = getCacheMap().get(key);
		return obj==null?defaultValue:obj;
	}

	public static void putCache(String key, Object value) {
		getCacheMap().put(key, value);
	}

	public static void removeCache(String key) {
		getCacheMap().remove(key);
	}
	
	public static Map<String, Object> getCacheMap(){
		Map<String, Object> map = Maps.newHashMap();
		try{
			Subject subject = SecurityUtils.getSubject();
			Principal principal = (Principal) subject.getPrincipal();
			return principal != null ? principal.getCacheMap():map;
		}catch (UnavailableSecurityManagerException e) {
			
		}catch (InvalidSessionException e){
			
		}
		return map;
	}
}
